import { defineStore } from 'pinia'
import { userLogin } from '../api/auth'

export const useUserStore = defineStore('user', {
    state: () => ({
        id: null,
        name: '',
        username: '',
        token: '',
        isSeller: 0
    }),

    getters: {
        isLogin: (state) => !!state.token,
    },

    actions: {
        // 用户登录
        async login(loginData) {
            try {
                console.log('开始登录，登录数据:', loginData)
                const data = await userLogin(loginData)
                console.log('登录结果数据:', data)

                // 可能返回了完整的响应，尝试提取data字段
                const userData = data.data || data

                if (userData) {
                    if (userData.token) {
                        console.log('设置token到localStorage:', userData.token)
                        localStorage.setItem('userToken', userData.token)
                    } else {
                        console.warn('警告: 登录响应中缺少token')
                    }
                } else {
                    console.error('登录响应数据无效')
                }

                this.setUserInfo(userData)
                // 保存完整用户信息到本地存储，供路由守卫检查卖家身份
                localStorage.setItem('user', JSON.stringify({
                    id: this.id,
                    name: this.name,
                    username: this.username,
                    isSeller: this.isSeller
                }))

                return Promise.resolve(userData)
            } catch (error) {
                console.error('登录失败:', error)
                return Promise.reject(error)
            }
        },

        // 设置用户信息
        setUserInfo(data) {
            if (!data) {
                console.error('无法设置用户信息，数据为空')
                return
            }

            console.log('设置用户信息:', data)
            this.id = data.id
            this.name = data.name || data.username
            this.username = data.username
            this.token = data.token
            this.isSeller = data.isSeller || 0

            // 保存token到localStorage
            if (data.token) {
                localStorage.setItem('userToken', data.token)
            } else {
                console.warn('警告: token为空')
            }

            // 更新本地存储的用户信息
            localStorage.setItem('user', JSON.stringify({
                id: this.id,
                name: this.name,
                username: this.username,
                isSeller: this.isSeller
            }))
        },

        // 更新卖家状态
        updateSellerStatus(status) {
            this.isSeller = status
            const userData = JSON.parse(localStorage.getItem('user') || '{}')
            userData.isSeller = status
            localStorage.setItem('user', JSON.stringify(userData))
        },

        // 退出登录
        logout() {
            this.id = null
            this.name = ''
            this.username = ''
            this.token = ''
            this.isSeller = 0
            localStorage.removeItem('userToken')
            localStorage.removeItem('user')
        }
    }
})